Time Visualizations

by_date = rat %>% 
  group_by(date) %>% 
  summarise(Total = n())

time_series = xts(by_date$Total , order.by= by_date$date)

hchart(time_series, name = "Rat Inspections") %>% 
  hc_credits(enabled = TRUE, text = "Sources: NYC OpenData", style = list(fontSize = "12px")) %>%
  hc_title(text = "Time Series of NYC Rat Inspections") %>%
  hc_legend(enabled = TRUE)
rat %>%
  mutate(inspection_year = as.factor(inspection_year)) %>%
  group_by(inspection_year) %>%
  summarise(n_obs = n()) %>%
  plot_ly(x = ~inspection_year, y = ~n_obs, type = "scatter", mode = "lines+markers") %>%
  layout(xaxis = list(title = "Year"), 
         yaxis = list(title = "Count"),
         title = "Number of Rat Inspections by Year") 
rat %>%
  mutate(inspection_month = as.factor(inspection_month)) %>%
  mutate(inspection_month = inspection_month %>% 
                       fct_relevel("Jan", "Feb", "Mar","Apr","May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")) %>%
  group_by(inspection_year, inspection_month) %>%
  summarise(n_obs = n()) %>%
  plot_ly(x = ~inspection_month, y = ~n_obs, type = "box",
          color = ~inspection_month) %>%
  layout(xaxis = list(title = "Month"), 
         yaxis = list(title = "Count"),
         title = "Number of Rat Inspections by Month") %>%
  hide_legend()
rat_2021 %>%
    mutate(inspection_day = as.factor(inspection_day)) %>%
  mutate(inspection_month = as.factor(inspection_month)) %>%
  mutate(inspection_month = inspection_month %>% 
                       fct_relevel("Jan", "Feb", "Mar","Apr","May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")) %>%
  separate(inspection_time, into = c("hour", "minute", "second")) %>%
  group_by(inspection_month, inspection_day, hour) %>%
  summarise(n_obs = n()) %>%
  plot_ly(x = ~hour, y = ~n_obs, type = "scatter",
          color = ~inspection_day,
          frame = ~inspection_month) %>%
  layout(xaxis = list(title = "Time", range = list(0,24), dtick = 3, 
                      tickvals = c(0, 3, 6, 9, 12, 15, 18, 21, 24),
                      ticktext = c("12am", "3am", "6am", "9am", "12pm", "3pm", "6pm", "9pm", "12am")),
         yaxis = list(title = "Count"),
         title = "Number of Rat Inspections Throughout a Day in 2021") %>%
  hide_legend()

Borough Visualizations

# Overall
rat %>%
 mutate(borough = as.factor(borough)) %>%
  rename(Year = inspection_year) %>%
  group_by(Year, borough) %>%
  summarise(n_obs = n()) %>%
  plot_ly(x = ~borough, y = ~n_obs, color = ~borough, frame = ~Year, type = "bar") %>%
  layout(xaxis = list(title = "Borough"), 
         yaxis = list(title = "Count"),
         title = "Number of Rat Inspections by Borough") %>%
  hide_legend()
# rat vs. population
p_rat = rat %>%
  filter(inspection_year == 2021) %>%
  mutate(borough = as.factor(borough)) %>%
  group_by(borough) %>%
  summarise(rat_obs = n()) %>%
  ggplot(aes(x = rat_obs, y = borough, fill = borough)) +
  geom_col(show.legend = FALSE) +
  scale_fill_manual(values = c("#38C5A3", "#F09968", "#8DA0CB", "#EE90BA", "#A8D14F")) +
  labs(x = "Count",
       y = "Borough",
      titles = "Number of Rat Inspections in 2021")

p_pop = rat %>%
  filter(inspection_year == 2021) %>%
  mutate(borough = as.factor(borough)) %>%
  group_by(borough) %>%
  summarise(rat_obs = n()) %>%
  mutate(pop = c(1424948, 2641052, 1576876, 2331143, 493494)) %>%
  ggplot(aes(x = pop, y = borough, fill = borough)) +
  geom_col(show.legend = FALSE) +
  scale_fill_manual(values = c("#38C5A3", "#F09968", "#8DA0CB", "#EE90BA", "#A8D14F")) +
  labs(x = "Count",
       y = "Borough",
       titles = "Number of Populations in 2021")

p_rat + p_pop 

# Overall top 15 streets
rat %>%
  filter(inspection_year == 2021) %>%
  mutate(street_name = str_to_title(street_name)) %>%
  group_by(street_name) %>%
  summarise(rat_obs = n()) %>%
  arrange(-rat_obs) %>% 
  top_n(20) %>%
  mutate(street_name = fct_reorder(street_name, rat_obs)) %>%
  plot_ly(x = ~rat_obs, y = ~street_name, color = ~street_name, type = "bar") %>%
  layout(xaxis = list(title = "Count"), 
         yaxis = list(title = "Street"),
         title = "Top 15 Rat Popular Streets in 2021") %>%
  hide_legend()
rat %>%
  filter(inspection_year == 2021) %>%
  mutate(street_name = str_to_title(street_name)) %>%
  group_by(borough, street_name) %>%
  summarise(rat_obs = n()) %>%
  arrange(-rat_obs) %>% 
  top_n(5) %>%
  mutate(street_name = fct_reorder(street_name, rat_obs)) %>%
  plot_ly(x = ~rat_obs, y = ~borough, color = ~street_name, type = "bar") %>%
  add_text(text=~street_name, hoverinfo='none', textposition = 'auto',
           textfont=list(size=5, color="black")) %>%
  layout(xaxis = list(title = "Count"), 
         yaxis = list(title = "Borough"),
         title = "Top 5 Rat Popular Street in Each Borough in 2021",
         height = 8, autosize=T) %>%
  hide_legend()
# inspection type
colors = c("#38C5A3", "#F09968", "#D2959B", "#B499CB", "#EE90BA", "#A8D14F", "#E5D800", "#FCCD4C", "#E2BF96", "#B3B3B3")

rat %>%
  mutate(inspection_type = case_when(inspection_type == "Initial" ~ "Initial Inspection",
                                     inspection_type == "BAIT" ~ "Baiting", 
                                     inspection_type == "Compliance"~ "Compliance Inspection", 
                                     inspection_type == "STOPPAGE" ~ "Stoppage", 
                                     inspection_type == "CLEAN_UPS" ~ "Clean Up")) %>%
  group_by(inspection_type) %>%
  summarise(n_obs = n(),
            prop = n_obs/nrow(rat)) %>%
  arrange(-prop) %>% 
  mutate(inspection_type = fct_reorder(inspection_type, -prop)) %>%
  plot_ly(labels = ~inspection_type, values = ~prop, type = "pie",
          insidetextfont = list(color = "#FFFFFF"),
          marker = list(colors = colors,
                      line = list(color = '#FFFFFF', width = 1))
          ) %>%
  layout(title = "Distribution of Inspection Type",
         xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
         yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
# inspection results
colors = c("#38C5A3", "#F09968", "#D2959B", "#B499CB", "#EE90BA", "#A8D14F", "#E5D800", "#FCCD4C", "#E2BF96", "#B3B3B3")

rat %>%
  drop_na() %>%
  group_by(result) %>%
  summarise(n_obs = n(),
            prop = n_obs/nrow(rat)) %>%
  arrange(-prop) %>% 
  mutate(result = fct_reorder(result, -prop)) %>%
  plot_ly(labels = ~result, values = ~prop, type = "pie",
          insidetextfont = list(color = "#FFFFFF"),
          marker = list(colors = colors,
                      line = list(color = '#FFFFFF', width = 1))
          ) %>%
  layout(title = "Distribution of Inspection Results",
         xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
         yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))

Weather and COVID-19

# association with temperature
rat_n = rat %>%
  group_by(inspection_year, inspection_month, inspection_day) %>%
  summarise(Count = n())

t = rat %>%
  mutate(t_avg = (tmin+tmax)/2) %>%
  distinct(inspection_year, inspection_month, inspection_day, t_avg, tmax, tmin, date)

rat_t = left_join(rat_n, t, by = c("inspection_year", "inspection_month", "inspection_day")) %>%
  as.data.frame()

rat_t %>% 
  filter(inspection_year == 2021) %>%
  ggplot(aes(x = date, y = t_avg)) + 
  geom_point(aes(size = Count), alpha = .5, color = "#EEADD5") +
  geom_smooth(se = FALSE, color = "#8DA0CB") +
  labs(x = "Month",
       y = "Average Temperature",
       title = "Assocition Between Number of Rat Inspections and Temperature")

# association with Covid-19
rat %>%
  filter(inspection_year %in% c(2019, 2020)) %>%
  mutate(inspection_year = as.factor(inspection_year)) %>%
  mutate(inspection_month = as.factor(inspection_month)) %>%
  mutate(inspection_month = inspection_month %>% 
                       fct_relevel("Jan", "Feb", "Mar","Apr","May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")) %>%
  group_by(inspection_year, inspection_month) %>%
  summarise(n_obs = n()) %>%
  plot_ly(x = ~inspection_month, y = ~n_obs, type = "scatter", mode = "lines",
          color = ~inspection_year) %>%
  layout(xaxis = list(title = "Inspection Month"), 
         yaxis = list(title = "Number of Inspections"),
         title = "Number of Rat Inspections Before and After COVID-19") 
# heatmap
nyc = rat_2021 %>%
  mutate(bbl = paste(boro_code, block, lot)) 

center_lon = median(nyc$longitude,na.rm = TRUE)
center_lat = median(nyc$latitude,na.rm = TRUE)

count = rat_2021 %>%
  mutate(bbl = paste(boro_code, block, lot)) %>%
  group_by(bbl) %>%
  count()

nyc = merge(nyc, count, by = "bbl")

factpal = colorFactor("blue", nyc$n)

nyc %>%
leaflet() %>% 
  addProviderTiles("CartoDB.Positron") %>%
  addHeatmap(lng = ~longitude, lat = ~latitude, intensity = ~(nyc$n), blur = 25, max = 0.05, radius = 15) %>%
  setView(lng=center_lon, lat=center_lat,zoom = 10)